package org.easybuy.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.easybuy.dao.NewsDao;
import org.easybuy.daoimpol.NewsDaoImopl;
import org.easybuy.entity.News;
import org.easybuy.util.Pages;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class NewsPageServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		String ID = request.getParameter("id");
		PrintWriter out = response.getWriter();
		if (ID == null) {
			String pageindex = request.getParameter("pageIndex");
			if (pageindex == null) {
				pageindex = "1";
			}
			Pages page2 = new Pages();
			NewsDao newsDao = new NewsDaoImopl();
			int totalCount;
			try {
				totalCount = newsDao.getTotalCount();
				page2.setPageSize(10);
				page2.setTotalCount(totalCount);
				int pageIndex = Integer.parseInt(pageindex);

				int totalPages = page2.getTotalPageCount();// 总页数
				int currPageNo = pageIndex;// 当前页数
				page2.setCurrPageNo(currPageNo);
				if (currPageNo > totalPages) {
					currPageNo = totalPages;
				}
				if (currPageNo < 1) {
					currPageNo = 1;

				}
				List<News> newList = new ArrayList<News>();
				try {
					newList = newsDao.getPageNewsList(page2.getCurrPageNo(),
							page2.getPageSize());
					page2.setNewsList(newList);
					String newsJSON = JSON.toJSONString(page2,
							SerializerFeature.WriteMapNullValue,
							SerializerFeature.WriteNullNumberAsZero,
							SerializerFeature.WriteNullStringAsEmpty);
					out.print(newsJSON);
					// request.setAttribute("totalPages", totalPages);
					// request.setAttribute("currPageNo", currPageNo);
					// request.setAttribute("list", newList);

				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			out.flush();
			out.close();
		} else if (ID != null) {
			String id = request.getParameter("id");
			NewsDao newsDao = new NewsDaoImopl();
			List<News> list = new ArrayList<News>();
			try {
				list = newsDao.getNewsById(Integer.parseInt(id));
				String newsJSON = JSON.toJSONString(list,
						SerializerFeature.WriteMapNullValue,
						SerializerFeature.WriteNullNumberAsZero,
						SerializerFeature.WriteNullStringAsEmpty);
				out.print(newsJSON);
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			out.flush();
			out.close();
		}

	}

}
